#coding=utf-8
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from events import Event
#from mysqldb import Mysql
import time,sys,common,json,configparser
class So360CommonCore:
   #配置项
    config = {
            "find_all":0,#0匹配多个1匹配一个就停
            "event":1,#0随机事件不发生，发生随机事件
            "click":1#0不点进去1点进去
    }
    keyword = ""#百度关键字
    clickWord = "";#点击关键字
    pages = 2;#刷的总页数
#times = 3000;#运行总次数
    success = 4;#运行成功多少次数才停止
    appuser = "dxseotj" #可靠性识别用，不用改
    userConfig = configparser.ConfigParser()
    userConfig.read('basic.conf');
    dataUrl = userConfig['url']['common_data_url']
    driver = '';# 驱动对象
    failTime = 0; #记录失败次数
    successTime = 0;#记录成功次数
    appTime = 0;#记录运行次数
    lastIP = '';#上次的ip
    vps_id = 0;#运行的vpsid
    type = 1;#搜索类型
    #初始化数据库
    #db = Mysql({"prefix":'qnh_',"host":'122.114.225.109',"user":"seotj","password":"seotj888","database":"seotj"})
    def __init__(self,**kwargs):
           
        if kwargs.get("keyword"): self.keyword = kwargs["keyword"]
        if kwargs.get("clickWord"): self.clickWord = kwargs["clickWord"]
        if kwargs.get("pages"): self.pages = kwargs["pages"]
        if kwargs.get("success"): self.success = kwargs["success"]
        if kwargs.get("vps_id"): self.vps_id = kwargs["vps_id"]
        if kwargs.get("type"): self.type = kwargs["type"]
        if kwargs.get("code"):
            self.code = kwargs["code"]
        else:
            self.code = '--'
        if kwargs.get("id"): self.id = kwargs["id"]
        return;
    #设置配置
    def setCfg(self,**kwargs):
        
        if kwargs.get("find_all"): self.config['find_all'] = kwargs["find_all"];
        if kwargs.get("event"): self.config['event'] = kwargs["event"];
        if kwargs.get("click"): self.config['click'] = kwargs["click"];
        return
    def run(self):
        flag =0 #flag= 1为成功
        isFind = 0 #1为找到了结果
        driver = self.driver
        
        driver.get("https://www.so.com/")
        #driver.maximize_window() #将浏览器最大化显示
        time.sleep(3)
        #driver.find_element_by_name("wd").send_keys(keyword)
        driver.find_element_by_id("input").send_keys(self.keyword)
        driver.find_element_by_id("search-button").click()
        
        time.sleep(6)
        event = Event({'driver':driver,'seo':'so360'})#随机
        page = 0;
        lineNum = 0 #当前页行数
        while page < self.pages:
            #找到一次记录就跳出

            if(isFind and self.config['find_all']==0):
                break;
            page = page+1;
            pageStr = '%d' %page;
            print('>>>第'+pageStr+'页');
            #触发百度列表的随机事件
            if(self.config['event']==1):
                event.random();
            # 使用xpath查找页面中的h3元素
            #t c-gap-bottom-small --- 百度经验
            res = driver.find_elements_by_xpath("//h3[@class='res-title']")  #
            linkNum = 0 #link条数
           
            for r in res:  # 页面中h3元素有多个，所以需要遍历
                lineNum = lineNum+1;
                #try:
                t = r.find_element_by_xpath("a")  # h3元素下的a标签
                #except:
                    #找不到的话继续下一条
                    #continue
                #print('%s - %s' % (t.text, type(t.text)))  # 打印a标签的标题以及文本格式
                get_it = 0#判断是否捕捉数据
                
                if(self.clickWord):
                    successCw = '';#命中的词语是
                    for cw in self.clickWord:
                        #if u''+cw in t.text:
                        if cw == t.text:
                            get_it =1
                            successCw  = cw 
                else:
                    #没有选中信息流就全选
                   get_it =1 
                if(get_it ==1):  # 判断标题中是否有keyword，如果有则点击
                    flag = 1
                    print('第'+pageStr+'页 :第'+str(lineNum)+'个数据yes')  # 判断结果
                    print('%s - %s' % (t.text, type(t.text)))
                    if(self.config['click']==1):
                        t.click()  # 点击这个a链接
                    add_time = int(time.time())
                                    
                    #db = Mysql({"prefix":'qnh_',"host":'122.114.225.109',"user":"seotj","password":"seotj888","database":"seotj"})
                    #db.insert(table='seo_log',data={'line_num':lineNum,'ip':lastIP,'add_time':add_time})    
                    #提交数据
                    if(self.clickWord):
                        sendWord = successCw
                    else:
                        sendWord = t.text
                    if(self.config['find_all']):
                        code = self.code+'-'+str(lineNum)
                    else:
                        code = self.code
                    data= {
                        'search_kw':self.keyword ,
                        'click_kws':sendWord,
                        'level':lineNum,
                        'appuser':self.appuser,
                        'code':code,
                        'id':self.id,
                        'client_id':self.vps_id
                    }
                    try:
                        post = common.getUrlRequest(self.dataUrl,data)
                        common.log(self.keyword+' '+post,'log.log')
                    except:
                        print('【!】数据发送失败');
                        #time.sleep(10)
                        #post = common.getUrlRequest(self.dataUrl,data)
                        common.log(self.keyword+' '+post,'error.log')
                    linkNum = linkNum+1
                    if(self.config['event']==1):
                        #详情页内事件
                        print('【详情页事件】');
                        event.upDown()
                        time.sleep(3)  
                    if(self.config['click']==1):
                        time.sleep(10)
                        #跳转回百度标签
                        handles = driver.window_handles
                        driver.switch_to_window(handles[0])
                        time.sleep(3)  
                    
                    #找到一次数据就跳出循环
                    isFind = 1;
                    if(isFind and self.config['find_all']==0):
                        break;
                    time.sleep(6)
            #触发百度列表的随机事件
            if(self.config['event']==1 and isFind == 1):
                print('【列表页事件】');
                event.random();
            if(isFind and self.config['find_all']==0):
                #如果找到了就退出循环
                break;
            linkNumStr = '%d' %linkNum;
            #print('>>>当前找到数据：'+linkNumStr)  # 打印h3标签的总量
            #下一页
            try:
                driver.find_element_by_link_text('下一页').click()
            except:
                #翻页翻到尽头了
                print('【!】翻到尽头也找不到数据');
            #driver.find_element_by_xpath("//a[@class='n']").click(); 
            
            time.sleep(6)
        
        driver.quit()
        #清除火狐缓存
        common.clearFfCache()
        return flag;
        #运行完睡400s
        
    def loop(self):
        result = 0;
        while self.successTime< self.success:
            print('{{开始执行关键字  ：【'+self.code +'】'+self.keyword +'}}');
            #ip = common.get_out_ip();
            #if(ip!=self.lastIP):
                #self.lastIP = ip
            #else:
                #print('ip没有变化，10s后重试');
                #time.sleep(10)
                #loop()
            
            
            #print('#### 当前ip：'+ip);
            
            
            self.appTime = self.appTime+1;
            
            self.driver = webdriver.Firefox()
            #driver = webdriver.Ie()
            # 设定页面加载限制时间
            self.driver.set_page_load_timeout(120)
            
            try:
                result = self.run()
                self.successTime = self.successTime+1;
                print('*')
                print('*')
                print('*')
                print('>>关键字  ：'+self.keyword +'');
                #print('>>循环次数：'+str(self.appTime)+'次');
                #print('>>成功次数：'+str(self.successTime)+'次');
                #print('#### 超时次数：'+str(self.failTime));
                
            except:
                self.failTime = self.failTime+1;
                failTimeStr = '%d' %self.failTime;
                print('*')
                print('*')
                print('*')
                print('请求超时了，15s后重试..已失败'+str(self.failTime)+'次')
                self.driver.quit()
                time.sleep(15)
                self.loop()
        print('完成')
        return result
        



if __name__ == '__main__':
  
    m  = core(keyword="YY学车",clickWord="有人听说过")
    m.loop()
#driver.quit()
#driver.back()
